<?php
namespace Users\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Db\Sql\Predicate;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Expression;
class TestimonialsTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function addTestimony($addData)
    {
		$data = array(
			'review_text' 		=> ucfirst($addData['testimony_text']), 				
			'status'           => 0,					
		);	
		if($addData['hid_testimony']!=""){
			$data1 = array(
				'updated_date' 	 => date('Y-m-d H:i:s'),
			 );
			 $data3= array_merge($data, $data1);
			$result=$this->tableGateway->update($data, array('review_id' => $addData['hid_testimony']));
		}else{
			if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']){
				$user_id=$_SESSION['user']['user_id'];
			}else{
				$user_id='';
			}
			 $data1 = array(
				'user_id' 	  	   => $user_id, 					
				'added_date' 		=> date('Y-m-d H:i:s'),  				
				'updated_date' 		=> date('Y-m-d H:i:s'),  	
			 );
			  $data3=array_merge($data, $data1);
			$result=$this->tableGateway->insert($data3);
		}		
		return $result;
    }	
	public function getcount($data='')
	{
		$select = $this->tableGateway->getSql()->select();
		if(isset($data['sSearch'])){
			if($data['sSearch']!='0' && $data['sSearch']!='')
			{
				$select->where(array(
					new Predicate\PredicateSet(
						array(
						new Predicate\Like('review_text', '%'.$data['sSearch'].'%'),
						),
							Predicate\PredicateSet::COMBINED_BY_OR
					),
				));
			}
		}
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet->count();
	}
	public function gettestinomialsGrid($searchkey,$aColumns,$data,$paginated=false)
	{
		$select = $this->tableGateway->getSql()->select();	
		$select->join('clf_users', 'clf_reviews.user_id=clf_users.user_id',array('username' =>'user_name'),'left');		
		// Search
		if(isset($data['sSearch'])){
			if($data['sSearch']!='0' && $data['sSearch']!='')
			{
				$select->where(array(
					new Predicate\PredicateSet(
						array(
						new Predicate\Like('review_text', '%'.$data['sSearch'].'%'),
						),
							Predicate\PredicateSet::COMBINED_BY_OR
					),
				));
			}
		}
		// End		
		// Sort Order
		if(isset($data['iSortCol_0']))
		{
			$sOrder="";
			for($i=0;$i<intval($data['iSortingCols']);$i++)
			{
				if($data['bSortable_'.intval($data['iSortCol_'.$i])] == "true")
				{
					$sOrder.=$aColumns[intval($data['iSortCol_'.$i])]." ".$data['sSortDir_'.$i].",";
				}
			}
			$sOrder = substr_replace($sOrder,"", -1);
		}		
		if($sOrder!=''){
			$select->order($sOrder);
		}
		// End		
		// Pagination
		if(isset($data['iDisplayStart']) && $data['iDisplayLength'] != '-1')
		{
			$start=(int)$data['iDisplayStart'];
			$currentPage = (int)$data['iDisplayLength'];
			$resultsPerPage = (int) $currentPage;
			$select->order('review_id DESC')->limit($currentPage)->offset($start);
		}
		// End	
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function editTestimony($review_id){
		$select = $this->tableGateway->getSql()->select();			
		$select->where('review_id="'.$review_id.'"');	
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->current();		
	}
	public function changeStatus($userIds,$value)
	{
		$data = array(
			'status'       =>$value,
		);
		$updateStatus=$this->tableGateway->update($data,array('(review_id IN ('.$userIds.'))'));
		return 	$updateStatus;			
	}
	public function testimonialsList($paginated=false){
		$select = $this->tableGateway->getSql()->select();
		$select->join('clf_users', 'clf_reviews.user_id=clf_users.user_id',array('*'),'left');
		$select->where('status="1"');		
		$resultSet = $this->tableGateway->selectWith($select);
		$paginatorAdapter = new DbSelect(
				$select,
				$this->tableGateway->getAdapter(),
				$resultSet
			);
		$paginator = new Paginator($paginatorAdapter);
		return $paginator;
			
	}
	public function deleteTestimony($testimony){
		$result= $this->tableGateway->delete(array('(review_id IN ('.$testimony.'))'));			
		return $result;
	}
}